<?php

namespace Analysis\Model;

use Think\Model;

class DiscModel extends Model
{
    const CODE_CLOSE = "Expired";
    const ORDER_REPEAT = "Repeat";
    const DISC_CLOSE = "Disc Cancel";
    const OVER_PLANE = "Over Plan";
    const MONEY_MATCH = "Money Plan";
    const MATCH = "Match.";

    protected $autoCheckFields = false;
    //
    private $user;
    private $order;
    private $total;
    private $code;
    //
    private $codeinfo;
    private $disinfo;

    public function checkCode($val)
    {
        $this->user = $val['user'];
        $this->order = $val['order'];
        $this->total = $val['total'];
        $this->code = $val['code'];
        return $this->checkCodeStatus();
    }

    /**
     * 检测代码是否失效
     */
    private function checkCodeStatus()
    {
        $disi = M("disisbn");
        $info = $disi->where("dis_isbn='" . $this->code . "'")->find();
        $this->codeinfo = $info;
        if ($info['dis_status'] != 1) {
            return $this->enOutCode(0, self::CODE_CLOSE);
        }
        return $this->checkOrderRepeat();
    }

    /**
     * 检测订单是否重复使用
     */
    private function checkOrderRepeat()
    {
        $couponhis = M('couponhis');

        $where["cou_orders"] = $this->order;
        $count = $couponhis->where($where)->count();
        if ($count != 0) {
            return $this->enOutCode(0, self::ORDER_REPEAT);
        }
        return $this->checkDiscStatus();
    }

    /**
     * 读取优惠信息检测优惠是否生效
     */
    private function checkDiscStatus()
    {
        $preftype = M("preftype");
        $info = $preftype->find($this->codeinfo['dis_preid']);
        $this->disinfo = $info;
        if ($info['pre_status'] != 1) {
            return $this->enOutCode(0, self::DISC_CLOSE);
        }
        switch ($info['pre_type']) {
            case '1': $final = $this->dealFullCutType($info['pre_type']);
                break;
            case '2': $final = $this->dealFullCutType($info['pre_type']);
                break;
            case '3': $final = $this->dealGiftType();
                break;
            case '4': $final = $this->dealAgioType();
                break;
            default: $final = $this->enOutCode(0, self::OVER_PLANE);
        }
        return $final;
    }

    /**
     * 优惠券信息查询
     */
    public function infoTrans($code)
    {
        if (!$this->codeStatus($code)) {
            return json_encode(array("status"=>0,"alt"=>"您的优惠码已失效.CS","dis"=>null));
        }
        if (!$this->discountStatus($this->codeinfo['dis_preid'])) {
            return json_encode(array("status"=>0,"alt"=>"您的优惠码已失效.DS","dis"=>null));
        }
        if (!$this->saleChenalStatus($this->codeinfo['dis_ssaid'])) {
            return json_encode(array("status"=>0,"alt"=>"您的优惠码已失效.SCS","dis"=>null));
        }
        $dis=array("t"=>$this->disinfo["pre_type"],"r"=>$this->disinfo["pre_trimoney"],'d'=>$this->disinfo["pre_ratio"]);
        return json_encode(array("status"=>1,"alt"=>$this->disinfo["pre_name"],"dis"=>$dis));
    }

    /*
     * 通用包
     */

    //检测代码是否失效
    private function codeStatus($val)
    {
        $disi = M("disisbn");
        $info = $disi->where("dis_isbn='" . $val . "'")->find();
        $this->codeinfo = $info;
        return $info['dis_status'] != 1 ? false : true;
    }

    //检测优惠是否失效
    private function discountStatus($val)
    {
        $preftype = M("preftype");
        $info = $preftype->find($val);
        $this->disinfo = $info;
        return $info['pre_status'] != 1 ? false : true;
    }

    //检测推广渠道是否关闭
    private function saleChenalStatus($val)
    {
        $salestaff = M("salestaff");
        $info = $salestaff->find($val);
        return $info['ssa_status'] != 1 ? false : true;
    }
    
    //处理满减/直降优惠类型
    private function dealFullCutType($t)
    {
        if ($this->total < $this->disinfo["pre_trimoney"]) {
            return $this->enOutCode(0, self::MONEY_MATCH);
        }
        /**
        if ($this->disinfo["pre_goods"] != NULL) {
            return 1;
        }
         */
        $amount = $this->total - $this->disinfo["pre_ratio"];
        return $this->enOutCode(1, self::MATCH, $t, $amount);
    }

    //处理赠品优惠类型
    private function dealGiftType()
    {
        if ($this->total < $this->disinfo["pre_trimoney"]) {
            return $this->enOutCode(0, self::MONEY_MATCH);
        }
        return $this->enOutCode(1, self::MATCH, 3, $this->disinfo["pre_goods"]);
    }

    //处理折扣优惠类型
    private function dealAgioType()
    {
        if ($this->total < $this->disinfo["pre_trimoney"]) {
            return $this->enOutCode(0, self::MONEY_MATCH);
        }
        /*
        if ($this->disinfo["pre_goods"] != NULL) {
            return 1;
        }
         */
        $agio = $this->total - $this->total * $this->disinfo["pre_ratio"];
        $autcl = $agio < $this->disinfo["pre_maxmoney"] ? $agio : $this->disinfo["pre_maxmoney"];
        $amount = $this->total - $autcl;
        return $this->enOutCode(1, self::MATCH, 4, $amount);
    }

    //输出函数
    private function enOutCode($status, $val, $type = null, $amount = null)
    {
        $couponhis = M("couponhis");
        $data = array(
            "cou_disid" => $this->codeinfo['dis_id'],
            "cou_user" => $this->user,
            "cou_orders" => $this->order,
            "cou_count" => $this->total,
            "cou_status" => $status,
            "cou_mark" => $val,
            "cou_time" => time()
        );
        $return = array("status" => $status, "value" => $val);
        $type != null ? $return["type"] = $type : null;
        $amount != null ? $return["amount"] = $amount : null;
        return $couponhis->add($data) ? json_encode($return) : $return;
    }
}
